package com.example.bpmervice.po;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.Collections;
import java.util.Date;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class TaskPo {
    private String id;
    private String name;
    private String taskDefinitionKey;
    private String assignee;
    private String processInstanceId;
    private String processDefinitionId;
    private Date createTime;
    private Map<String, Object> variables;
    private String businessKey;
    private String processDefinitionName;

    // 安全转换方法
    public static TaskPo fromTask(Task task, RuntimeService runtimeService,
                                  RepositoryService repositoryService) {
        TaskPo dto = new TaskPo();
        dto.setId(task.getId());
        dto.setName(task.getName());
        dto.setTaskDefinitionKey(task.getTaskDefinitionKey());
        dto.setAssignee(task.getAssignee());
        dto.setProcessInstanceId(task.getProcessInstanceId());
        dto.setProcessDefinitionId(task.getProcessDefinitionId());
        dto.setCreateTime(task.getCreateTime());

        // 安全地获取变量
        dto.setVariables(getVariablesSafely(runtimeService, task.getProcessInstanceId()));

        // 获取业务信息和流程定义名称
        setBusinessInfo(dto, runtimeService, repositoryService, task.getProcessInstanceId());

        return dto;
    }

    private static Map<String, Object> getVariablesSafely(RuntimeService runtimeService, String processInstanceId) {
        try {
            return runtimeService.getVariables(processInstanceId);
        } catch (Exception e) {
            return Collections.singletonMap("error", "无法加载变量: " + e.getMessage());
        }
    }

    private static void setBusinessInfo(TaskPo dto, RuntimeService runtimeService,
                                        RepositoryService repositoryService, String processInstanceId) {
        try {
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .singleResult();

            if (processInstance != null) {
                dto.setBusinessKey(processInstance.getBusinessKey());

                // 获取流程定义名称
                ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                        .processDefinitionId(processInstance.getProcessDefinitionId())
                        .singleResult();
                if (processDefinition != null) {
                    dto.setProcessDefinitionName(processDefinition.getName());
                }
            }
        } catch (Exception e) {
            // 忽略错误，保持其他数据可用
            log.error("无法获取业务信息: {}", e.getMessage(), e);
        }
    }
}
